﻿<script src="https://cdn.bootcss.com/moment.js/2.24.0/moment.js"></script>
<script>
var StaffCode="CN437233";
var requrl = "/gtsventure/bmsmodular/customapi/RetrieveQueryDocs";
var TotalScoreItem=10;
var ProjectList=[];
var ScoreItemList=[];
var ProjectScoreItemsByGroup=[];
var ScoreItems=[
        { text: '品牌 3.8', max: 5 },
        { text: '内容', max: 5 },
        { text: '可用性', max: 5 },
        { text: '功能', max: 5 },
        { text: '张欧川子', max: 5 }
];

var radarChatData= [
    {
        value: [0, 0, 0, 0, 0],
        name: 'Staff'
    }
];


function getUser(){
    var url="/gtsventure/bmsmodular/customapi/GetUserInfo";
    var Jsondata = {
        staffCode:StaffCode,
        organizationCode:"PwCCNHK"
    };
    $.ajax({
        url: url,
        type: 'POST',
        data: Jsondata,
        async: true,
        success: function (response) {
            console.log("getUser",response);
        }
    });
}


function GetScoreItemList(){
    var Jsondata = {};
    Jsondata.SearchItem = [];
    Jsondata.EntityName = "4ba7xugno4u";
    Jsondata.index = 0;
    Jsondata.size = 100;    
    Jsondata.SearchItem.push({ "Name": "IsDeleted", "Value": 0, "Operator": 'Equal', 'Method': 'AND' });    
    Jsondata.SearchItem.push({ "Name": "TargetUserId", "Value": StaffCode, "Operator": 'Equal', 'Method': 'AND' });    
    $.ajax({
        url: requrl,
        type: 'POST',
        data: Jsondata,
        async: false,
        success: function (response) {
            if (response.StatusCode === 200) {
                if (response.Data.total > 0) { 
                    $.each(response.Data.rows,function(index,item){
                        ScoreItemList.push(item);
                    });
                    console.log("GetScoreItemList",ScoreItemList);
                }
            }
            return [];
        }
    });
    return ScoreItemList;
};


function GetProjectList(){
    
    var tempProjectList=[];
    $.each(ScoreItemList,function(index,o){
        var flag=false;
        for(i=0;i<tempProjectList.length;i++){
            if(tempProjectList[i]==o.ProjectId){flag=true;break;}
        };
        if(!flag)tempProjectList.push(o.ProjectId);
    });
    console.log("tempProjectList",tempProjectList);
    

    var ProjectList=[];

    var Jsondata = {};
    Jsondata.SearchItem = [];
    Jsondata.EntityName = "7m2ncu0h5i";
    Jsondata.index = 0;
    Jsondata.size = 100;
    Jsondata.Columns =["_id","Status"];
    Jsondata.SearchItem.push({ "Name": "_id", "Value": tempProjectList.join("|"), "Operator": 'Contains', 'Method': 'AND' });
    $.ajax({
        url: requrl,
        type: 'POST',
        data: Jsondata,
        async: false,
        success: function (response) {
            if (response.StatusCode === 200) {
                if (response.Data.total > 0) { 
                    $.each(response.Data.rows,function(index,item){
                        var obj={
                             ProjectId:item._id
                            ,Status:item.Status
                        };
                        ProjectList.push(obj);
                       
                    });
                    console.log("GetProjectList",ProjectList);
                }
            }
            return [];
        }
    });
    return ProjectList;
};


function GetProjectScoreItems(){
    var tempProjectList=GetProjectList();    
    $.each(tempProjectList,function(index,o){
        var tempList=[];
        var obj;
        $.each(ScoreItemList,function(i,k){
            if(k.ProjectId==o.ProjectId){
                tempList.push(k);
                obj={
                    ProjectId:k.ProjectId,
                    ProjectName:k.ProjectName,
                    TargetUserId:k.TargetUserId,
                    TargetUserName:k.TargetUserName,
                    ProjectStatus:o.Status
                };
            }
        });

        obj.count=tempList.length;
        obj.Creaters=[];
        for(i=1;i<=TotalScoreItem;i++){
            obj["ScoreItem"+i.toString()]=0;
            obj["ScoreItemCount"+i.toString()]=0;
            obj["ScoreItemAverage"+i.toString()]=0;
        }
        $.each(tempList,function(i,k){
            obj.Creaters.push({
                CreateById:k["C4-CreateById"],
                CreateBy:k["C4-CreateBy"]
            });
            for(i=1;i<=TotalScoreItem;i++){
                var vl=GetScore(k["ScoreItem"+i.toString()]);
                obj["ScoreItem"+i.toString()]+=vl;
                obj["ScoreItemCount"+i.toString()]+=vl==0?0:1;
            }
            
        });
        for(i=1;i<=TotalScoreItem;i++){
            var score=obj["ScoreItem"+i.toString()];
            var count=obj["ScoreItemCount"+i.toString()];
            var Divisor=count==0?1:count;
            obj["ScoreItemAverage"+i.toString()]=(score/Divisor).toFixed(1)/1;
        }
        
        ProjectScoreItemsByGroup.push(obj);
    });

    console.log("ProjectScoreItemsByGroup",ProjectScoreItemsByGroup);

    

    var tpRadar={};
    for(i=1;i<=TotalScoreItem;i++){
        tpRadar["ScoreTotal"+i.toString()]=0;
        tpRadar["ScoreCount"+i.toString()]=0;
    }


    $.each(ProjectScoreItemsByGroup,function(index,o){
        for(i=1;i<=TotalScoreItem;i++){
            tpRadar["ScoreTotal"+i.toString()]+=o["ScoreItemAverage"+i.toString()];
            var count=o["ScoreItemCount"+i.toString()];
            tpRadar["ScoreCount"+i.toString()]+=count==0?0:1;
        }
        
    });
    
    for(i=1;i<TotalScoreItem;i++){
        var count=tpRadar["ScoreCount"+i.toString()];
        count=count==0?1:count;
        var total=tpRadar["ScoreTotal"+i.toString()];
        tpRadar["tpRadar"+i.toString()]=(total/count).toFixed(1)/1;
    }
    console.log("tpRadar",tpRadar);

    var radarData=[];
    console.log("radarData",radarData);
};



function IsNotNull(value) {
    return value !== "" && value !== null && value !== undefined ? true : false;
};

function GetScore(value){
    return value !== "" && value !== null && value !== undefined ? value : 0;
}

$(function(){
    GetScoreItemList();
    GetProjectScoreItems();
    getUser();
});
</script>